package com.magina.antiPro;

import com.magina.antiPro.dto.TvsSeriesDTO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by cass on 2018/7/11.
 */
@RestController
@RequestMapping("/tvseries")
public class TvSeriesController {
    private static final Log log = LogFactory.getLog(TvSeriesController.class);
    @GetMapping
    public Map<String,Object> sayHello(){
        Map<String,Object> map = new HashMap<>();
        map.put("message", "hello world!");
        return map;
    }
    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/tvserieses")
    public List<TvsSeriesDTO> getAll(){
        if(log.isTraceEnabled()){
            log.trace("getAll被调用了-----");
        }
        List<TvsSeriesDTO> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(2016, Calendar.OCTOBER, 2, 0, 0);
        list.add(new TvsSeriesDTO(1,"WestWorld",1,calendar.getTime()));
        calendar.set(2011, Calendar.SEPTEMBER, 2, 0, 0);
        list.add(new TvsSeriesDTO(2, "Person of Interest", 5, calendar.getTime()));
        if(log.isTraceEnabled()){
            log.trace("传入参数:" + list.get(0).getName());
        }
        return list;
    }

    @GetMapping("/{id}")
    public TvsSeriesDTO getOne(@PathVariable int id ){
        if(log.isTraceEnabled()){
            log.trace("getOne-------" + id);
        }
        if (id == 102) {
            return createPoi();
        }else if(id == 21){
            return createWestworld();
        }else{
            throw new ResourceNotFoundException();
        }
    }

    @PostMapping
    public TvsSeriesDTO insertOne(@RequestBody TvsSeriesDTO tvsSeriesDTO){
        if(log.isTraceEnabled()){
            log.trace("insertOne----"+tvsSeriesDTO.toString());
        }
        tvsSeriesDTO.setId(99999);
        return tvsSeriesDTO;
    }

    @DeleteMapping("/{id}")
    public Map<String,Object> deleteOne(@PathVariable int id,
                                        HttpServletRequest request,
                                        @RequestParam(value = "del_reason",required = false) String deleteReason){
        if(log.isTraceEnabled()) {
            log.trace("deleteOne-------" + id);
        }
        Map<String,Object> map = new HashMap<>();
        if(id == 101){
            map.put("message","#101被" + request.getRemoteAddr() + "删除(原因" + deleteReason + ")");
        }else if(id == 102){
            throw new RuntimeException("#102不能被删除");
        }else{
            throw new ResourceNotFoundException();
        }
        return map;
    }

    private TvsSeriesDTO createPoi(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(2016, Calendar.SEPTEMBER, 22, 0, 0);
        return new TvsSeriesDTO(102,"我和僵尸有个约会",32,calendar.getTime());
    }

    private TvsSeriesDTO createWestworld(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(2109,Calendar.OCTOBER,11,0,0);
        return new TvsSeriesDTO(21,"West World",2,calendar.getTime());
    }
}
